/*
 * @Name: 
 * @Author: LiuCaiLin
 * @Date: 2020-04-19 21:59:12
 * @LastEditors: LiuCaiLin
 * @LastEditTime: 2020-05-09 18:31:27
 */
import {
	request
} from '../../utils/request.js'
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		detail: '',
		desc_text: '',
		show: false,
		status: '',
		ids: '',
		clickTrue: true,
		pre: 0,
		totalMoney: 0,
		number: 1,
		stock: 0,
		num: 0,
    cartNumber:0
	},

	/**
	 * 生命周期函数--监听页面加载
	 */

	onLoad: function (options) {
				this.goodDetails(options.id)
		this.setData({
			ids: options.id,
      cartNumber: wx.getStorageSync("cartNumber")
		})
    console.log(wx.getStorageSync("cartNumber"))
	},

	onShow() {
		this.sendRule()
	},
	onClickIcon() {
		wx.switchTab({ url: '/pages/shopCart/shopCart' });
	},

	onClickButton() {
		Toast('点击按钮');
	},
	collect() {
		this.setData({
			show: !this.data.show
		})
		if (this.data.show) {
			this.setData({
				status: 1
			})
			wx.showToast({
				title: '收藏成功', //提示的内容,
				icon: 'none', //图标,
				duration: 1500, //延迟时间,

			});
		} else {
			this.setData({
				status: 2
			})
			wx.showToast({
				title: '取消收藏成功', //提示的内容,
				icon: 'none', //图标,
				duration: 1500, //延迟时间,

			});
		}

		request({
			url: `collect`,
			// url: `collect?product_id=32`,
			method: 'POST',
			data: {
				product_id: this.data.ids,
				status: this.data.status
			},
			token: wx.getStorageSync('token')
		}).then(res => {

		})

	},
	goodDetails(id) {
		request({
			url: `goodsDetail?id=${id}`,
			method: 'POST',
			token: wx.getStorageSync('token')
		}).then(res => {

			if (res.data.data.isFavorite == 1) {
				//未收藏
				this.setData({
					show: false,
					status: 2
				})
			} else {
				this.setData({
					show: true,
					status: 1
				})
			}
			if (res.data.data.desc_text) {
				var desc_text = res.data.data.desc_text.replace(/<img /g, '<img class="rich_img" ')
			}
			let deti = []
			deti.push(res.data.data)
			this.setData({
				detail: deti,
				stock: res.data.data.stock,
				num: res.data.data.number,
				desc_text: desc_text
			})
		})
	},
	add(e) {
		let number = this.data.number
		let stock = 0
		number = this.data.number + 1
		stock = this.data.stock - 1
		this.setData({
			number,
			stock
		})

	},
	minus(e) {
		let number = this.data.number
		let stock = 0
		number = this.data.number - 1
		stock = this.data.stock + 1
		if (number <= 0) {
			wx.showToast({
				title: '商品数量最少为1哦', //提示的内容,
				icon: 'none',
				duration: 2000, //延迟时间,
				mask: true, //显示透明蒙层，防止触摸穿透,
				success: res => {
					number = 1
				}
			});
			return
		}
		this.setData({
			number,
			stock
		})

	},

	/** 
	 * 加入购物车
	*/
	addCart() {
		let num = 0
  		num = this.data.number + this.data.num
		request({
			url: `setNum`,
			token: wx.getStorageSync('token'),
			method: 'POST',
			data: {
				productId: this.data.ids,
				number: num
			}
		}).then(res => {
			if (res.data.error_code == 0) {
				wx.showToast({
					title: '加入购物车成功', //提示的内容,
					icon: 'none', //图标,
					duration: 1500, //延迟时间,

				});
				
        let cartNumber = this.data.cartNumber
        cartNumber = parseInt(this.data.cartNumber) + parseInt(this.data.number)
        this.setData({
					number: 1,
          cartNumber: cartNumber
				})
				// wx.navigateTo({ url: `/pages/foodsDetail/foodsDetail?id=${this.data.ids}` });
			}
		})
	},

	/** 
	 * 立即购买
	*/
	createConsultation() {
			this.data.detail[0].number = this.data.number

		wx.navigateTo({
			url: `/pages/pay/pay?orderInfo=${JSON.stringify(this.data.detail)}`
		})


	},

	getPreOrder(token, orderID) {
		if (token) {
			request({
				url: `/pay/pre_order`,
				token: token,
				method: 'POST',
				data: {
					id: orderID
				}
			}).then(res => {
							var preData = res.data;
						wx.requestPayment({
					timeStamp: preData.timeStamp.toString(),
					nonceStr: preData.nonceStr,
					package: preData.package,
					signType: preData.signType,
					paySign: preData.paySign,
					success: function (res) {
						wx.hideLoading();
						wx.navigateTo({ url: '/pages/payTwo/payTwo' });
					},
					fail: function (error) {
						wx.hideLoading();
									wx.showModal({
							title: '提示',
							content: '订单未支付（30分钟内订单不支付，订单将自动取消，请尽快支付）',
							success: function (res) {
								if (res.confirm) {
									wx.navigateTo({ url: '/pages/payOne/payOne' });
								} else if (res.cancel) {

								}
							}
						})
					}
				})
			})

		}
	},

	/** 
	 * 满多少包邮
	*/
	sendRule() {
		request({
			url: `sendRule`,
			token: wx.getStorageSync('token'),
			method: 'POST',
		}).then(res => {
			this.setData({
				pre: res.data.money
			})
		})

	},

  onUnload:function(){
    wx.setStorageSync('repair', 1)

    app.scanCart()
  }

})
